using System;
using System.Collections.Generic;
using System.Text;
using Db4objects.Db4o;
using System.Runtime.Remoting.Lifetime;

namespace Midas.Core
{
	abstract public class ManagerBase: MarshalByRefObject, IDisposable
	{
		internal User loggedInAs;

		/// <summary>
		/// User the Manager is ... Managing, duh?
		/// </summary>
		internal User LoggedInAs
		{
			get { return loggedInAs; }
			set { loggedInAs = value; }
		}

		/// <summary>
		/// Returns true if it is logged in and ready to receive commands
		/// </summary>
		public bool IsReadyForUse
		{
			get { return loggedInAs != null && IsLeaseValid; }
		}

		/// <summary>
		/// Returns true if the lease is valid
		/// </summary>
		public bool IsLeaseValid
		{
			get
			{
				ILease lease = (ILease)this.GetLifetimeService();
				return lease.CurrentState != LeaseState.Expired && lease.CurrentState!= LeaseState.Null;
			}
		}
		protected IObjectContainer client;
		public ManagerBase()
		{
			client = DatabaseManager.DatabaseServer.OpenClient();
		}

		~ManagerBase()
		{
			((IDisposable)this).Dispose();
		}

		void IDisposable.Dispose()
		{
			this.client.Close();
		}
	}
}
